﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cocos2D;
using CCPoint = Cocos2D.CCPoint;

namespace TestTiger.PlayingCard
{
    public enum CardColor
    {
        黑桃=0,
        樱花,
        红桃,
        方片,
        背面
    }

    public class PokerCard:CCSprite
    {
        /// <summary>
        /// Cards数字
        /// A==1 .....K==13
        /// </summary>
        public int CardNumber{ get; private set; }
        /// <summary>
        /// 扑克牌花色
        /// </summary>
        public CardColor  Color { get; private set; }
        /// <summary>
        /// 13 = A+方片
        /// </summary>
        public int CardID { get; private set; }
        public bool IsVisble { get; set; }

        public int Height
        {
            get { return Texture != null ? this.Texture.PixelsHigh : 0; }
        }
        public int Width
        {
            get { return Texture != null ? Texture.PixelsWide : 0; }
        }

        /// <summary>
        /// 扑克牌
        /// </summary>
        /// <param name="texture2D">图片</param>
        /// <param name="pt">坐标</param>
        /// <param name="cardId">牌号</param>
        /// <param name="visble">是否显示</param>
        public PokerCard(CCTexture2D texture2D, CCPoint pt, int cardnumber,CardColor color,bool visble):base(texture2D)
        {
            Position = pt;
            CardNumber = cardnumber;
            Color = color;
            //红桃3 =  32
            CardID = cardnumber + 10 + (int) color;
            IsVisble = visble;           
        }
    }

    public class PlayingCards:CCSprite
    {
        /// <summary>
        /// 扑克牌图片列表
        /// </summary>
        protected List<PokerCard> Cards;
        /// <summary>
        /// 所有的扑克牌集合
        /// </summary>
        protected PokerCard[,] AllCards; 

        /// <summary>
        /// 当前坐标
        /// </summary>
        public CCPoint Loacation { get; private set; }

        /// <summary>
        /// 扑克牌面最终的结果
        /// 11==J
        /// 12==Q
        /// 13==K 
        /// </summary>
        public int ResoutNumber { get; set; }
        /// <summary>
        /// 结果花色
        /// </summary>
        public CardColor ResoutColor { get; set; }

        /// <summary>
        /// 当前的扑克牌
        /// </summary>
        public PokerCard CurrentCard { get; private set; }

        /// <summary>
        /// 是否是停止状态
        /// </summary>
        public bool IsStop { get; private set; }


        #region 时间和状态控制
        /// <summary>
        /// 当前的状态
        /// 0 == 停止
        /// 1 == 加速
        /// 2 == 减速
        /// </summary>
        private byte _state = 0;

        private float useTime;
        #endregion

        #region 速度控制

        //最大速度
        private float _maxspeed;

        /// <summary>
        /// 最大速度
        /// </summary>
        public float MaxSpeed
        {
            get { return _maxspeed; }
            set { if (value > _lowspeed) _maxspeed = value; }
        }

        //最慢速度
        private float _lowspeed;

        /// <summary>
        /// 最慢速度
        /// </summary>
        public float LowSpeed
        {
            get { return _lowspeed; }
            set { if (_lowspeed < _maxspeed) _lowspeed = value; }
        }

        //当前速度
        private float _speed;
        //速度增长/减小因子
        private float _speedfactor;

        /// <summary>
        /// 加减速因子
        /// </summary>
        public float SpeedFactor
        {
            get { return _speedfactor; }
            set { if (value > 0) _speedfactor = value; }
        }

        #endregion

       

        /// <summary>
        /// 当滚动到指定结果后触发
        /// </summary>
        public event EventHandler OnScrollFinsish;


        Random rd = new Random();
        private CCPoint nextCardPosition;

        public PlayingCards(CCTexture2D[] cards, CCPoint pt)
        {
            Loacation = pt; //记录坐标
            //this.Cards = cards.ToList();
            this.Cards = new List<PokerCard>(3);
            this.AllCards = new PokerCard[14, 4];

            nextCardPosition = new CCPoint(pt.X, pt.Y - cards[0].PixelsHigh);

            //创建扑克牌列表对象，默认只显示第一张
            for (int i = 0; i < cards.Length; i++)
            {
                if (i == 0)
                {
                    var zeroCard = new PokerCard(cards[0], pt, 0,CardColor.背面, true);
                    AllCards[0, 0] = zeroCard;
                    AllCards[0, 1] = zeroCard;
                    AllCards[0, 2] = zeroCard;
                    AllCards[0, 3] = zeroCard;
                }
                else
                {
                    AllCards[i%4, (int) (i/4)] = new PokerCard(
                        cards[i],
                       nextCardPosition,
                        i%4,
                        (CardColor) ((int) (i/4)),
                        false);
                }
            }
            Cards.Add(AllCards[0, 0]); //当前扑克牌
            Cards.Add(AllCards[1, rd.Next(0, 4)]); //下一张
            //设置当前的显示扑克牌
            CurrentCard = Cards[0];
        }

        public void ChangeSpeed()
        {
            switch (_state)
            {
                case 1: //加速
                    if (_speed + _speedfactor < _maxspeed)
                        _speed += _speedfactor;
                    else
                    {
                        _speed = _maxspeed;
                    }
                    break;

                case 2: //减速

                     if (_speed - _speedfactor > _lowspeed)
                        _speed -= _speedfactor;
                    else
                    {
                        _speed = _lowspeed;
                    }
                    break;
            }
        }

        /// <summary>
        /// 卡牌滚动的逻辑
        /// </summary>
        public void ChangeCard()
        {
            var currenCardModel = Cards[0];
            var nextCard = Cards[1];
            nextCard.IsVisble = true;
            currenCardModel.Position = new CCPoint(currenCardModel.Position.X, currenCardModel.Position.Y - _speed);
            nextCard.Position = new CCPoint(nextCard.Position.X, nextCard.Position.Y - _speed);

            if (currenCardModel.Position.Y >= Loacation.Y + currenCardModel.Height)
            {
                Cards.RemoveAt(0);
                //随机一个花色
                var color = rd.Next(0, 4);
                var next =  AllCards[currenCardModel.CardNumber == 13 ? 1 : currenCardModel.CardNumber + 1, color];
                Cards.Add(next);
                currenCardModel.IsVisble = false;
                //设置位置
                currenCardModel.Position = nextCardPosition;
            }
            var heightY = 20;
            CurrentCard =
                Cards.First(
                    op => op.IsVisble && op.Position.Y >= Loacation.Y + heightY || op.Position.Y <= Loacation.Y - heightY);
        }

        
        public override void Update(float dt)
        {
            base.Update(dt);
            if (_state == 0) return;
            ChangeSpeed();
            ChangeCard();
            switch (_state)
            {
                case 1:
                    if (_speed .Equals(_maxspeed))
                    {
                        useTime += dt;
                        if (useTime >= 15*1000)
                           StopWithRandom(); //设置随机停止结果
                    }
                    break;
                case 2:
                    if(_speed.Equals(_lowspeed))
                        if (CurrentCard.CardNumber == ResoutNumber && CurrentCard.Color==ResoutColor)
                        { 
                            CurrentCard.RunAction(new CCMoveTo(1f, Position));
                        }
                    break;
            } 
        }


        public void StartLoop()
        {
            _state = 1;//状态改为加速
        }

        private void CheckCardAndReadyToSlowDown(int resout)
        {
            if (_state != 1) return;
             
        }

        /// <summary>
        /// 将扑克牌停在制定的结果上
        /// </summary>
        /// <param name="resout"></param>
        public void StopWithResout(int resout,CardColor color)
        {
            _state = 2;//设置为减速
            ResoutNumber = resout;
            ResoutColor = color;
        }

        /// <summary>
        /// 将扑克牌停止在随机的结果处
        /// </summary>
        public void StopWithRandom()
        {
            StopWithResout(rd.Next(1, 13), (CardColor) rd.Next(0, 4));
        }

    }
}
